import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface Activity {
    id: string;
    title: string;
    description: string;
    location: string;
    date: string;
    time: string;
    capacity: number;
    participants: number;
    tags: string[];
    images: string[];
    creatorId: string;
    createdAt: string;
}

interface ActivityState {
    activities: Activity[];
    loading: boolean;
    error: string | null;
    selectedActivity: Activity | null;
}

const initialState: ActivityState = {
    activities: [],
    loading: false,
    error: null,
    selectedActivity: null,
};

const activitySlice = createSlice({
    name: 'activity',
    initialState,
    reducers: {
        fetchActivitiesStart: (state) => {
            state.loading = true;
            state.error = null;
        },
        fetchActivitiesSuccess: (state, action: PayloadAction<Activity[]>) => {
            state.activities = action.payload;
            state.loading = false;
            state.error = null;
        },
        fetchActivitiesFailure: (state, action: PayloadAction<string>) => {
            state.loading = false;
            state.error = action.payload;
        },
        createActivityStart: (state) => {
            state.loading = true;
            state.error = null;
        },
        createActivitySuccess: (state, action: PayloadAction<Activity>) => {
            state.activities.push(action.payload);
            state.loading = false;
            state.error = null;
        },
        createActivityFailure: (state, action: PayloadAction<string>) => {
            state.loading = false;
            state.error = action.payload;
        },
        selectActivity: (state, action: PayloadAction<Activity>) => {
            state.selectedActivity = action.payload;
        },
        clearSelectedActivity: (state) => {
            state.selectedActivity = null;
        },
    },
});

export const {
    fetchActivitiesStart,
    fetchActivitiesSuccess,
    fetchActivitiesFailure,
    createActivityStart,
    createActivitySuccess,
    createActivityFailure,
    selectActivity,
    clearSelectedActivity,
} = activitySlice.actions;

export default activitySlice.reducer; 